﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;


namespace FYP.Profilings
{
    public class PerformanceData
    {
        private Game _game;

        private int _noOfPoints;
        private int _noOfCharacters;
        private int _noOfTriangles;

        private int _totalUPS;
        private int _totalFPS;

        private double _timeStart = -1;
        private double _timeEnd = -1;

        private double _averageFPS;
        private double _averageUPS;
        private double _totalMiliseconds;

        double presetDuration = -1;

        #region Construction
        /// <summary>
        /// Initializes a new instance of the PerformanceData class.
        /// </summary>
        public PerformanceData(Game game)
        {
            _game = game;
        }

        public PerformanceData()
        {
        }
        #endregion

        /// <summary>
        /// This will increase the TotalUPS and check if the process should beend or not
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            if (IsEnded) return;
            _totalUPS++;
            if (presetDuration > 0 && presetDuration < gameTime.TotalRealTime.TotalMilliseconds - _timeStart)
            {
                End(gameTime);
            }
        }

        public void Draw(GameTime gameTime)
        {
            if (IsEnded) return;
            _totalFPS++;
        }

        public void SetStopTime(double duration)
        {
            presetDuration = duration;
        }

        public void Start(GameTime gameTime)
        {
            _timeStart = gameTime.TotalRealTime.TotalMilliseconds;
        }

        public void End(GameTime gameTime)
        {
            _timeEnd = gameTime.TotalRealTime.TotalMilliseconds;

            // Set some statistic variables
            _totalMiliseconds = _timeEnd - _timeStart;
            _averageFPS = _totalFPS / (_totalMiliseconds / 1000); // Divide _totalMiliseconds to 1000 to convert to seconds
            _averageUPS = _totalUPS / (_totalMiliseconds / 1000);
        }

        #region Dynamic properties
        [XmlIgnore]
        public bool IsStarted
        {
            get
            {
                if (_timeStart > -1)
                {
                    return true;
                }
                return false;
            }
        }
        [XmlIgnore]
        public bool IsEnded
        {
            get
            {
                if (_timeEnd > -1)
                {
                    return true;
                }
                return false;
            }
        }
        #endregion


        #region Properties
        [XmlIgnore]
        public Game Game
        {
            get { return _game; }
            set { _game = value; }
        }
        public int NoOfCharacters
        {
            get { return _noOfCharacters; }
            set { _noOfCharacters = value; }
        }
        public int NoOfTriangles
        {
            get { return _noOfTriangles; }
            set { _noOfTriangles = value; }
        }
        public int NoOfPoints
        {
            get { return _noOfPoints; }
            set { _noOfPoints = value; }
        }
        public double AverageFPS
        {
            get { return _averageFPS; }
            set { _averageFPS = value; }
        }
        public double AverageUPS
        {
            get { return _averageUPS; }
            set { _averageUPS = value; }
        }
        public double TotalMiliseconds
        {
            get { return _totalMiliseconds; }
            set { _totalMiliseconds = value; }
        }
        public double TimeStart
        {
            get { return _timeStart; }
            set { _timeStart = value; }
        }
        public double TimeEnd
        {
            get { return _timeEnd; }
            set { _timeEnd = value; }
        }
        public int TotalFPS
        {
            get { return _totalFPS; }
            set { _totalFPS = value; }
        }
        public int TotalUPS
        {
            get { return _totalUPS; }
            set
            {
                _totalUPS = value;
            }
        }
        #endregion
    }
}
